//
//  DMToolsObject.m
//  新项目
//
//  Created by Gamin on 2016/12/20.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "DMToolsObject.h"

@implementation DMToolsObject

/**
 判断出不同维修类型，从而展示不同图片
 */
+ (NSString *)judgeDataTypeFrom:(NSString *)theTitle {
    NSString *retStr = @"0";
    // ios8以后的方法
    if([theTitle containsString:@"美容"]){
        retStr = @"1";
    }else if([theTitle containsString:@"保养"]){
        retStr = @"2";
    }else if([theTitle containsString:@"维修"]){
        retStr = @"3";
    }
    return retStr;
}

+ (id)changeNullDataWith:(id)theData andDefault:(id)theDefault {
    id returnData;
    if([[theData class] isSubclassOfClass:[NSNull class]]){
        returnData = theDefault;
    }else{
        returnData = theData;
    }
    return returnData;
}

// 时间戳——yyyy年M月d日
+ (NSString *)cStringFromTimestamp:(NSString *)timestamp {
    // 时间戳转时间的方法
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    // [dateFormatter setDateFormat:@"yyyy年M月d日 H:mm"];
    [dateFormatter setDateFormat:@"yyyy年M月d日"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}
// 时间戳--yyyy-MM-dd
+ (NSString *)cStringFromTimestampType:(NSString *)timestamp {
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}
// 时间戳--yyyy-MM
+ (NSString *)cStringYearAndMonth:(NSString *)timestamp {
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}
// 时间戳--MM月dd日
+ (NSString *)cStringFromTimestampMD:(NSString *)timestamp {
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM月dd日"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}
// 时间戳--MM日dd HH:mm
+ (NSString *)cStringFromTimestampMDHM:(NSString *)timestamp {
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM月dd HH:mm"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}
// 时间戳--yyyy-MM-dd HH:mm
+ (NSString *)cStringFromTimestampYMDHM:(NSString *)timestamp {
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}
// 时间戳--HH:mm
+ (NSString *)cStringFromTimestampHM:(NSString *)timestamp {
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}

// 当前时间的时间戳
+ (long int)cNowTimestamp {
    NSDate *newDate = [NSDate date];
    long int timeSp = (long)[newDate timeIntervalSince1970];
    return timeSp;
}

// 根据时间戳转换为需要的时间
+ (NSString *)getNeedTimestampString:(NSString *)theModelTime {
    NSString *tempTime = [DMToolsObject cStringFromTimestampMD:theModelTime];
    // 数据返回日期 yyyy-MM-dd
    NSString *old_ymd  = [DMToolsObject cStringFromTimestampType:theModelTime];
    NSString *old_hm   = [DMToolsObject cStringFromTimestampHM:theModelTime];
    // 今天
    NSString *nowTimes = [NSString stringWithFormat:@"%ld",[DMToolsObject cNowTimestamp]];
    NSString *now_ymd  = [DMToolsObject cStringFromTimestampType:nowTimes];
    if([now_ymd isEqualToString:old_ymd]){
        // tempTime = [NSString stringWithFormat:@"今天%@",old_hm];
        tempTime = [NSString stringWithFormat:@"%@",old_hm];
    }else{
        // 昨天
        NSString *yesTimes = [NSString stringWithFormat:@"%ld",[DMToolsObject cNowTimestamp]-(24*60*60)];
        NSString *yes_ymd  = [DMToolsObject cStringFromTimestampType:yesTimes];
        if([yes_ymd isEqualToString:old_ymd]){
            tempTime = [NSString stringWithFormat:@"昨天%@",old_hm];
        }else{
            // 前天
            NSString *befTimes = [NSString stringWithFormat:@"%ld",[DMToolsObject cNowTimestamp]-(24*60*60)*2];
            NSString *bef_ymd  = [DMToolsObject cStringFromTimestampType:befTimes];
            if([bef_ymd isEqualToString:old_ymd]){
                tempTime = [NSString stringWithFormat:@"前天%@",old_hm];
            }
        }
    }
    
    return tempTime;
}

// 字符串时间转为时间戳
+ (NSString *)getTimestampWithStringTime:(NSString *)theTimestamp {
    theTimestamp = [theTimestamp stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    // 设置时间显示格式: 2011-01-26 17:40:50
    NSString* timeStr = theTimestamp;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    // 设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    // 设置时区,这个对于时间的处理有时很重要
    // 例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
    // 例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
    // 他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate* datenow = [formatter dateFromString:timeStr];
    // 时间转时间戳的方法:
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];

    return timeSp;
}

// 替换字典中null对象
+ (NSMutableDictionary *)replaceNullObjctWithDict:(NSDictionary *)objDict {
    NSMutableDictionary *replaceDict = [[NSMutableDictionary alloc] init];
    if(objDict){
        NSArray *allKeys = [objDict allKeys];
        if(allKeys){
            for (int i = 0; i < allKeys.count ; i ++) {
                NSString *tempKey = [allKeys objectAtIndex:i];
                id tempObj = [objDict objectForKey:tempKey];
                if(tempObj == nil || [[tempObj class] isSubclassOfClass:[NSNull class]]){
                    tempObj = @"";
                }
                [replaceDict setObject:tempObj forKey:tempKey];
            }
        }
    }
    return replaceDict;
}


@end
